package Twice.day12;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * @Author: lya
 * @Description: 本类的作用描述:第二种排序方式
 * @CreateDate: 2019/8/7
 * @Version: 1.0
 */
public class TreeSetSortDemo {
    public static void main(String[] args) {
        Book b1 = new Book(1,"西a游记","吴承恩",100.0);
        Book b2 = new Book(2,"水浒传","施耐庵",150.0);
        Book b3 = new Book(3,"红a楼梦","曹雪芹",80.0);
        Book b4 = new Book(4,"三国演义","罗贯中",70.0);
        Book b5 = new Book(5,"等a一个人的咖啡","九把刀",120.0);
        Book b6 = new Book(6,"毛泽东讲义","毛润之",120.0);

        //在构建TreeSet对象的同时传入一个比较器
        TreeSet<Book> books = new TreeSet<>(new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                if (o1.getPrice()>o2.getPrice()){
                    return -1;
                }else if(o1.getPrice()<o2.getPrice()){
                    return 1;
                }
                return 0;
            }
        });

        books.add(b1);
        books.add(b2);
        books.add(b3);
        books.add(b4);
        books.add(b5);
        books.add(b6);
    }

}
